package com.tongji.housediy.service
{
	import com.tongji.housediy.events.GetHouseByIDEvent;
	import com.tongji.housediy.events.designEvent.SaveDeleteDesignEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.vo.House;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/11
	 */ 
	public class HouseService extends Actor
	{
		public var houseRemoteObject:RemoteObject;
		
		public function HouseService()
		{
			super();
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.0.118/housediy/Gateway.aspx")];
			var amfChannel:Array = [new AMFChannel(null, "http://design.rryjw.com/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediy/Gateway.aspx")];
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");

			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			houseRemoteObject = new RemoteObject("fluorine");
			houseRemoteObject.source = "HouseDiyService.HouseDiyAction";
			houseRemoteObject.showBusyCursor = true;
			houseRemoteObject.channelSet = channelSet;
			
			houseRemoteObject.getOperation("getHouses").addEventListener(ResultEvent.RESULT, getAllHouseResultHandler);
			houseRemoteObject.getOperation("getHouses").addEventListener(FaultEvent.FAULT, faultHandler);
		
			houseRemoteObject.getOperation("getSampleHouses").addEventListener(ResultEvent.RESULT, getSampleHandler);
			houseRemoteObject.getOperation("getSampleHouses").addEventListener(FaultEvent.FAULT, faultHandler);
			
			houseRemoteObject.getOperation("getHouseByCantavilID").addEventListener(ResultEvent.RESULT, getHouseByCantavilIDHandler);
			houseRemoteObject.getOperation("getHouseByCantavilID").addEventListener(FaultEvent.FAULT, faultHandler);
			
			houseRemoteObject.getOperation("getHouseDetail").addEventListener(ResultEvent.RESULT, getHouseByIDHandler);
			houseRemoteObject.getOperation("getHouseDetail").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function getHouseArray(pageIndex:int, itemCount:int):void
		{
			houseRemoteObject.getHouses(pageIndex, itemCount);
		}
		
		public function getSampleHouseArr(pageIndex:int, itemCount:int):void
		{
			houseRemoteObject.getSampleHouses(pageIndex, itemCount);
		}
		
		public function getHouseByCantavil(pageIndex:int, itemCount:int, cantavilID:int):void
		{
			houseRemoteObject.getHouseByCantavilID(pageIndex, itemCount, int(cantavilID));
		}		
		
		public function getHouseByID(houseID:int):void
		{
			houseRemoteObject.getHouseDetail(houseID);

		}
		/*---------------------返回值处理函数-----------------------------------*/		
		private function getAllHouseResultHandler(event:ResultEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.UPDATE_HOUSE_MODEL, 0, 0, 0, event.result as ArrayCollection));
		}
		
		private function getSampleHandler(event:ResultEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.UPDATE_SAMPLE_HOUSE, 0, 0,0, event.result as ArrayCollection));
		}
		
		private function getHouseByCantavilIDHandler(event:ResultEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.UPDATE_HOUSE_MODEL, 1, 6,0, event.result as ArrayCollection));
		}
		
		private function getHouseByIDHandler(event:ResultEvent):void
		{
			dispatch(new GetHouseByIDEvent(GetHouseByIDEvent.UPDATE_HOUSE_DETAIL, 0, event.result as ArrayCollection));
		}
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}